from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, JsonResponse
from rest_framework import viewsets
# Create your views here.
from .models import Category, Book
from .serializers import CategorySerializer, BookSerializer
from rest_framework.decorators import api_view
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView, ListCreateAPIView, RetrieveUpdateDestroyAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \
	DestroyModelMixin


class CategoryViewSet(viewsets.ModelViewSet):
	queryset = Category.objects.all()
	# 通过序列化类 指定序列化查询集的规则
	serializer_class = CategorySerializer


# 通过api_view 把视图函数变为了视图类 并且指定父类是APIView
@api_view(["GET", "POST"])
def category_list(request):
	"""
	instance 数据库已经有的数据
	data 表单传递的数据
	"""
	if request.method == "GET":
		cs = Category.objects.all()
		seria = CategorySerializer(instance=cs, many=True)
		return Response(seria.data, status=status.HTTP_200_OK)
	elif request.method == "POST":
		seria = CategorySerializer(data=request.data)
		if seria.is_valid(raise_exception=True):
			seria.save()
			return Response(seria.data, status=status.HTTP_201_CREATED)


@api_view(["GET", "PUT", "DELETE"])
def category_detail(request, pk):
	c = get_object_or_404(Category, id=pk)
	if request.method == "GET":
		serai = CategorySerializer(instance=c)
		return Response(serai.data, status=status.HTTP_200_OK)
	elif request.method == "PUT":
		seria = CategorySerializer(instance=c, data=request.data)
		if seria.is_valid(raise_exception=True):
			seria.save()
			return Response(seria.data, status=status.HTTP_200_OK)
	elif request.method == "DELETE":
		c.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)


class CategoryListAPIView(APIView):
	def get(self, request):
		cs = Category.objects.all()
		seria = CategorySerializer(instance=cs, many=True)
		return Response(seria.data, status=status.HTTP_200_OK)

	def post(self, request):
		seria = CategorySerializer(data=request.data)
		if seria.is_valid(raise_exception=True):
			seria.save()
			return Response(seria.data, status=status.HTTP_201_CREATED)


class CategoryDetailAPIView(APIView):
	def get(self, request, pk):
		c = get_object_or_404(Category, id=pk)
		seria = CategorySerializer(instance=c)
		return Response(seria.data, status=status.HTTP_200_OK)

	def put(self, request, pk):
		c = get_object_or_404(Category, id=pk)
		seria = CategorySerializer(instance=c, data=request.data)
		if seria.is_valid(raise_exception=True):
			seria.save()
			return Response(seria.data, status=status.HTTP_200_OK)

	def delete(self, request, pk):
		c = get_object_or_404(Category, id=pk)
		c.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)


class ListMixin:
	def list(self, request):
		seria = self.get_serializer(self.get_queryset(), many=True)
		return Response(seria.data, status=status.HTTP_200_OK)


class CreateMixin:
	def create(self, request):
		seria = self.get_serializer(data=request.data)
		if seria.is_valid(raise_exception=True):
			seria.save()
			return Response(seria.data, status=status.HTTP_201_CREATED)


class RetrieveMixin:
	def retrieve(self, request, pk):
		seria = self.get_serializer(instance=self.get_object())
		return Response(seria.data, status=status.HTTP_200_OK)


class UpdateMixin:
	def update(self, request, pk):
		seria = self.get_serializer(instance=self.get_object(), data=request.data)
		if seria.is_valid(raise_exception=True):
			seria.save()
			return Response(seria.data, status=status.HTTP_200_OK)


class DeleteMixin:
	def destroy(self, request, pk):
		self.get_object().delete()
		return Response(status=status.HTTP_204_NO_CONTENT)


class CategoryListGenericAPIView(GenericAPIView, ListModelMixin, CreateModelMixin):
	queryset = Category.objects.all()
	serializer_class = CategorySerializer

	def get(self, request):
		return self.list(request)

	def post(self, request):
		return self.create(request)


class CategoryDetailGenericAPIView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
	queryset = Category.objects.all()
	serializer_class = CategorySerializer

	def get(self, request, pk):
		return self.retrieve(request, pk)

	def put(self, request, pk):
		return self.update(request, pk)

	def delete(self, request, pk):
		return self.destroy(request, pk)


# 视图集
# 视图类（一个模型要写两个视图类）
class CategoryListGenericMixinAPIView(ListCreateAPIView):
	queryset = Category.objects.all()
	serializer_class = CategorySerializer


class CategoryDetailGenericMixinAPIView(RetrieveUpdateDestroyAPIView):
	queryset = Category.objects.all()
	serializer_class = CategorySerializer


# class CategoryViewSetMixinGenericAPIView(viewsets.GenericViewSet, ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin):
class CategoryViewSetMixinGenericAPIView(viewsets.ModelViewSet):

	queryset = Category.objects.all()
	serializer_class = CategorySerializer



class BookViewSet(viewsets.ModelViewSet):
	queryset = Book.objects.all()
	serializer_class = BookSerializer